#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2021/9/30 9:51'
# software: PyCharm

import time

from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse

from apps.web import models
from apps.web.forms import project

from utils.tencent import cos


def project_list(request):
    """
    项目列表
    :param request:
    :return:
    """
    if request.method == "GET":
        # GET请求查看项目列表
        project_dict = {"star": [], "my": [], "join": []}
        # 查询当前用户创建的所有项目
        my_project_list = models.Project.objects.filter(creator=request.tracer.user)
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({"value": row, "type": "my"})
            else:
                project_dict['my'].append(row)

        # 当前用户参与的所有项目
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({"value": item.project, "type": "join"})
            else:
                project_dict['join'].append(item.project)
        form = project.ProjectModeForm(request)
        return render(request, 'project_list.html', {"form": form, "project_dict": project_dict})
    form = project.ProjectModeForm(request, data=request.POST)
    if form.is_valid():
        # 为项目创建一个桶
        bucket = "{0}-{1}-1253858492".format(request.tracer.user.mobile_phone, str(int(time.time())))
        region = "ap-guangzhou"
        cos.create_bucket(bucket, region)
        # 创建项目,把桶和区域写入到数据库
        form.instance.bucket = bucket
        form.instance.region = region
        # 验证通过:用户提交了项目名、颜色、描述，还需要谁创建的项目
        form.instance.creator = request.tracer.user
        instance = form.save()  # 创建项目成功后返回当前创建项目的对象
        # 项目初始化问题类型
        issues_type_object_list = []
        for item in models.IssuesType.PROJECT_INIT_LIST:  # ["任务", '功能', 'Bug']
            issues_type_object_list.append(models.IssuesType(project=instance, title=item))
        models.IssuesType.objects.bulk_create(issues_type_object_list)  # 批量添加
        return JsonResponse({'status': True})

    return JsonResponse({"status": False, "error": form.errors})


def project_star(request, project_type, project_id):
    """
    点击添加星标项目
    :param request:
    :return:
    """
    if project_type == "my":
        models.Project.objects.filter(pk=project_id, creator=request.tracer.user).update(star=True)
        return redirect("project_list")

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')

    return HttpResponse('请求错误')


def project_off_star(request, project_type, project_id):
    """
    取消星标
    :param request:
    :param project_type:
    :param project_id:
    :return:
    """
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')

    if project_type == 'join':
        models.ProjectUser.objects.filter(project_id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')

    return HttpResponse('请求错误')
